package com.code.generator.engine;


import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Map;
import org.apache.commons.io.FileUtils;

public class FreemarkerEngine {
    public FreemarkerEngine() {
    }

    public static void createFileByFTL(Configuration cfg, Map<String, Object> root, String ftlName, String descFile) {
        String encoding = (String)root.get("encoding");
        String userCustomCode = "";

        try {
            Template template = cfg.getTemplate(ftlName, encoding);
            File file = new File(descFile);
            if(!file.exists()) {
                FileUtils.forceMkdir(file.getParentFile());
                file.createNewFile();
                System.out.println("生成文件:" + file.getAbsolutePath());
            } else {
                String os = "";
                BufferedReader out = null;
                Object fileReader = null;

                try {
                    InputStreamReader e1 = new InputStreamReader(new FileInputStream(file), encoding);
                    out = new BufferedReader(e1);
                    boolean startFlag = false;
                    boolean isFirst = true;

                    while((os = out.readLine()) != null) {
                        if(os.contains("user customize code start")) {
                            startFlag = true;
                        } else if(os.contains("user customize code end")) {
                            startFlag = false;
                        } else if(startFlag) {
                            if(isFirst) {
                                userCustomCode = userCustomCode + os;
                                isFirst = false;
                            } else {
                                userCustomCode = userCustomCode + "\r\n" + os;
                            }
                        }
                    }
                } catch (Exception var19) {
                    var19.printStackTrace();
                } finally {
                    if(fileReader != null) {
                        ((FileReader)fileReader).close();
                    }

                    if(out != null) {
                        out.close();
                    }

                }

                System.out.println("替换文件:" + file.getAbsolutePath());
            }

            FileOutputStream os1 = new FileOutputStream(file);
            OutputStreamWriter out1 = new OutputStreamWriter(os1, encoding);
            root.put("userCustomCode", userCustomCode);
            template.process(root, out1);
            out1.flush();
            out1.close();
            os1.close();
        } catch (IOException var21) {
            var21.printStackTrace();
            throw new RuntimeException(var21);
        } catch (TemplateException var22) {
            var22.printStackTrace();
            throw new RuntimeException(var22);
        }
    }
}
